/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.header.DateHeader;
import java.util.Calendar;

/**
 * The Date header field reflects the time when the request or response is
 * first sent. Retransmissions have the same Date header field value as the
 * original. The Date header field contains the date and time. Unlike
 * HTTP/1.1, SIP only supports the most recent
 * <a href = "http://www.ietf.org/rfc/rfc1123.txt">RFC 1123</a> format for dates.
 * SIP restricts the time zone in SIP-date to "GMT", while RFC 1123 allows any
 * time zone.
 * <p/>
 * The Date header field can be used by simple end systems without a
 * battery-backed clock to acquire a notion of current time. However, in its
 * GMT form, it requires clients to know their offset from GMT.
 * <p/>
 * Example:<br>
 * Date: Sat, 13 Nov 2010 23:29:00 GMT
 *
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class DateHeaderImpl extends HeaderBase implements DateHeader {
    private Calendar date;

    private static final DateHeaderParser parser = new DateHeaderParser();

    public DateHeaderImpl() {
        super(DateHeader.NAME);
    }

    public DateHeaderImpl(byte[] data, int start, int end) {
        super(DateHeader.NAME, data, start, end);
    }

    public Object clone() {
        final DateHeaderImpl header = new DateHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.date = this.date;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return date != null ? parser.formatDate(date.getTime()) : "<unknown>";
    }

    /**
     * Sets date of DateHeader. The date is repesented by the Calendar object.
     *
     * @param date the Calendar object date of this header.
     */
    public void setDate(Calendar date) {
        parse();
        this.date = date;
        invalidateHeaderData();
    }

    /**
     * Gets the date of DateHeader. The date is repesented by the Calender
     * object.
     *
     * @return the Calendar object representing the date of DateHeader
     */
    public Calendar getDate() {
        parse();
        return this.date;
    }
}
/*
 * $Log$
 */